package com.muki.spider;

import com.muki.spider.exception.SpiderMoudleOverArrayException;
import com.muki.spider.moudle.Moudle;
import com.muki.spider.moudle.recycle.MukiSpiderRecycle;
import com.muki.spider.moudle.statistics.MukiSpiderStatistics;

/**
 * 模块控制器
 * 单例模式
 * @author ljh_2017
 *
 */
public class MoudleController {

	private static final MoudleController controller = new MoudleController();
	private Moudle[] moudles = new Moudle[2];
	
	public static MoudleController getInstance() {
		return controller;
	}
	
	public MoudleController() {
		init();
	}
	
	public void init() {
		moudles[0] = new MukiSpiderRecycle();
		moudles[1] = new MukiSpiderStatistics();
	}
	
	public void setMoudle(int index,Moudle moudle) {
		moudles[index] = moudle;
	}
	
	public Moudle getMoudle(int index) throws SpiderMoudleOverArrayException {
		if(index>moudles.length)
			throw new SpiderMoudleOverArrayException();
		return moudles[index];
	}
	
	public void runSpiderToMoudle(int index,Spider spider) {
		moudles[index].init();
		moudles[index].run(spider);
		moudles[index].destroy();
	}
	
	
}
